package com.example.aqarapp.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import java.text.SimpleDateFormat;
import java.util.TimeZone;
import java.util.Timer;
import java.util.TimerTask;

public class CirclePgBar extends View {


    private Paint mBackPaint;
    private Paint mFrontPaint;
    private TextPaint mTextPaint;
    private float mStrokeWidth = 20;
    private float mHalfStrokeWidth = mStrokeWidth / 2;
    private float mRadius = 100;
    private RectF mRect;
    private int mProgress = 0;
    //目标值，想改多少就改多少
    private int mTargetProgress = 100;
    private int mMax = 100;       //转动的最大次数，应该设为最终计时多长时间------------------------------------------------------
    private int mWidth;
    private int mHeight;

    private Timer timer;//定时器对象
    private TimerTask timerTask;//执行定时器的线程（要调度的任务）
    private int TimeInterval = 1000;//定时器执行时间间隔
    private int TimeStart = 0;//定时器启动时的延迟时间

    private boolean mIsPause = false;




    public CirclePgBar(Context context) {
        super(context);
        init();
    }

    public CirclePgBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CirclePgBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    //完成相关参数初始化
    private void init() {
        mBackPaint = new Paint();
        mBackPaint.setColor(Color.WHITE);
        mBackPaint.setAntiAlias(true);
        mBackPaint.setStyle(Paint.Style.STROKE);
        mBackPaint.setStrokeWidth(mStrokeWidth);

        mFrontPaint = new Paint();
        mFrontPaint.setColor(Color.BLUE);
        mFrontPaint.setAntiAlias(true);
        mFrontPaint.setStyle(Paint.Style.STROKE);
        mFrontPaint.setStrokeWidth(mStrokeWidth);


        mTextPaint = new TextPaint();
        mTextPaint.setColor(Color.GREEN);
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextSize(20);
        mTextPaint.setTextAlign(Paint.Align.CENTER);

    }


    //重写测量大小的onMeasure方法和绘制View的核心方法onDraw()
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = getRealSize(widthMeasureSpec);
        mHeight = getRealSize(heightMeasureSpec);
        setMeasuredDimension(mWidth, mHeight);

    }

    public void pause(boolean isPause) {
        mIsPause = isPause;
    }

    public void stop() {
        if(timer !=null ) {
            timer.cancel();
            timer = null;
        }
        if(timerTask != null) {
            timerTask.cancel();
            timerTask = null;
        }
        mProgress = 0;
        mIsPause = false;
    }

    public void start() {
        startTimer();
        timer.schedule(timerTask,TimeStart,TimeInterval);
    }

    public void startTimer() {
        if(timer == null) timer = new Timer();
        if (timerTask == null) timerTask = new TimerTask() {
            @Override
            public void run() {
                if(!mIsPause) {
                    if (mProgress < mMax) {
                        mProgress += 1;
                        invalidate();
                    }
                    else{
                        if(timer !=null ) timer.cancel();
                        if(timerTask != null) timerTask.cancel();
                    }
                }
            }
        };
    }

    public void setMaxValue(int max) {
        mMax = max;
    }




    @Override
    protected void onDraw(Canvas canvas) {
        initRect();
        float angle = mProgress / (float) mMax * 360;
        canvas.drawCircle(mWidth / 2, mHeight / 2, mRadius, mBackPaint);
        canvas.drawArc(mRect, -90, angle, false, mFrontPaint);
        int second = mMax-mProgress;
//        canvas.drawText(timeConversion(second) + "后关闭", mWidth / 2 + mHalfStrokeWidth, mHeight / 2 + mHalfStrokeWidth, mTextPaint);
        StaticLayout myStaticLayout = new StaticLayout(timeConversion(second) + "后关闭", mTextPaint, 80, Layout.Alignment.ALIGN_LEFT, 1.0f, 5.0f, true);

        canvas.translate(mWidth / 2, mHeight / 2 - mHalfStrokeWidth * 2);
        myStaticLayout.draw(canvas);
    }

    public String timeConversion(int time) {
        int hour = 0;
        int minutes = 0;
        int sencond = 0;
        int temp = time % 3600;
        if (time > 3600) {
            hour = time / 3600;
            if (temp != 0) {
                if (temp > 60) {
                    minutes = temp / 60;
                    if (temp % 60 != 0) {
                        sencond = temp % 60;
                    }
                } else {
                    sencond = temp;
                }
            }
        } else {
            minutes = time / 60;
            if (time % 60 != 0) {
                sencond = time % 60;
            }
        }
        return (hour<10?("0"+hour):hour) + ":" + (minutes<10?("0"+minutes):minutes) + ":" + (sencond<10?("0"+sencond):sencond);
    }

    public int getRealSize(int measureSpec) {
        int result = 1;
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);

        if (mode == MeasureSpec.AT_MOST || mode == MeasureSpec.UNSPECIFIED) {
            //自己计算
            result = (int) (mRadius * 2 + mStrokeWidth);
        } else {
            result = size;
        }

        return result;
    }

    private void initRect() {
        if (mRect == null) {
            mRect = new RectF();
            int viewSize = (int) (mRadius * 2);
            int left = (mWidth - viewSize) / 2;
            int top = (mHeight - viewSize) / 2;
            int right = left + viewSize;
            int bottom = top + viewSize;
            mRect.set(left, top, right, bottom);
        }
    }


}
